﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Topro.Util
{
	/// <summary>
	/// IEnumerable的Distinct扩展方法
	/// 当集合元素为对象时，可用于元素对象指定字段进行排重集
	/// </summary>
	public static class DistinctExtensions
	{
		public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
		{
			return source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
		}

		public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source,
				Func<T, V> keySelector, IEqualityComparer<V> comparer)
		{
			return source.Distinct(new CommonEqualityComparer<T, V>(keySelector, comparer));
		}
	}

	public class CommonEqualityComparer<T, V> : IEqualityComparer<T>
	{
		private Func<T, V> keySelector;
		private IEqualityComparer<V> comparer;

		public CommonEqualityComparer(Func<T, V> keySelector, IEqualityComparer<V> comparer)
		{
			this.keySelector = keySelector;
			this.comparer = comparer;
		}

		public CommonEqualityComparer(Func<T, V> keySelector) : this(keySelector, EqualityComparer<V>.Default)
		{ }

		public bool Equals(T x, T y)
		{
			return comparer.Equals(keySelector(x), keySelector(y));
		}

		public int GetHashCode(T obj)
		{
			return comparer.GetHashCode(keySelector(obj));
		}
	}
}
